using System;
using System.Collections.Generic;
using System.Text;
using FlatRedBall;
using FlatRedBall.Input;
using FlatRedBall.Graphics;
using FlatRedBall.AI.Pathfinding;
using FlatRedBall.Graphics.Animation;
using FlatRedBall.Graphics.Particle;

using FlatRedBall.Graphics.Model;
using FlatRedBall.Math.Geometry;
using FlatRedBall.Math.Splines;

using Cursor = FlatRedBall.Gui.Cursor;
using GuiManager = FlatRedBall.Gui.GuiManager;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Input;

#if FRB_XNA || SILVERLIGHT
using Keys = Microsoft.Xna.Framework.Input.Keys;
using Vector3 = Microsoft.Xna.Framework.Vector3;
using Texture2D = Microsoft.Xna.Framework.Graphics.Texture2D;
using Gnome.Engine;
#endif

namespace Gnome.Screens
{
	public partial class MainMenu
	{
        int selected;

        const float TIME_FOR_TRANSITIONS = 0.67f;
        bool transition;
        float transitionTimeSoFar;
        bool transitionIn;
        float transitionInTimeSoFar;


		public void CustomInitialize()
		{
            GlobalData.is2P = true;
            transition = false;
            transitionIn = true;
            transitionTimeSoFar = 0.0f;
            transitionInTimeSoFar = 0.0f;

            Cursor.Y = 2f;
            Cursor.X = -1;

            Cursor.ScaleX = LevelBase.SCREEN_WIDTH / 3 + 3;
            Cursor.ScaleY = LevelBase.SCREEN_HEIGHT / 14;//0.25f;

            MenuItems.ScaleX = LevelBase.SCREEN_WIDTH / 2;
            MenuItems.ScaleY = LevelBase.SCREEN_HEIGHT / 2;
            MenuItems.Z = Cursor.Z + 1;
            SpriteManager.AddToLayer(MenuItems, OverlayLayer);
		}

		public void CustomActivity(bool firstTimeCalled)
		{
            if (transitionIn)
            {
                transitionInTimeSoFar += TimeManager.SecondDifference;
                float percentLeft = transitionInTimeSoFar / TIME_FOR_TRANSITIONS;

                MenuItems.Alpha = percentLeft;
                MainImageBackground.Alpha = percentLeft;
                Cursor.Alpha = percentLeft;

                if (transitionInTimeSoFar > TIME_FOR_TRANSITIONS)
                {
                    transitionIn = false;
                }
            }

            if (transition)
            {
                transitionTimeSoFar += TimeManager.SecondDifference;

                float percentLeft = 1 - transitionTimeSoFar / TIME_FOR_TRANSITIONS;
                MenuItems.Alpha = percentLeft;
                MainImageBackground.Alpha = percentLeft;
                Cursor.Alpha = percentLeft;


                if (transitionTimeSoFar > TIME_FOR_TRANSITIONS)
                {
                    transition = false;
                    IsActivityFinished = true;
                    switch (selected)
                    {
                        case 0:
                            Console.WriteLine("Play");
                            GlobalData.is2P = false;
                            MoveToScreen(typeof(Gnome.Screens.LevelOne).FullName);
                            Microsoft.Xna.Framework.Media.MediaPlayer.Stop();
                            break;
                        case 1:
                            GlobalData.is2P = true;
                            MoveToScreen(typeof(Gnome.Screens.LevelOneMult).FullName);
                            Microsoft.Xna.Framework.Media.MediaPlayer.Stop();
                            break;

                        case 2:
                            Console.WriteLine("Instructions");
                            MoveToScreen((typeof(Gnome.Screens.Instructions).FullName));
                            break;
                        case 3:
                            Console.WriteLine("Credits");
                            MoveToScreen((typeof(Gnome.Screens.Credits).FullName));
                            break;
                        case 4:
                            Console.WriteLine("Exit");
                            Environment.Exit(0);
                            break;
                        default:
                            Console.WriteLine("ERRORORORROROROROROR");
                            break;
                    }
                }
            }
            else
            {
                if (CustomInputManager.isMenuSelect())
                {
                    transition = true;
                }
                else if (CustomInputManager.isMenuDown())
                {
                    //Move down list but up from play -> about -> etc
                    selected++;
                    if (selected > 4)
                        selected = 0;
                    drawIcon();
                }
                else if (CustomInputManager.isMenuUp())
                {
                    selected--;
                    if (selected < 0)
                        selected = 4;
                    drawIcon();
                }
            }
		}
        private void drawIcon()
        {
            Cursor.Y = 2f - selected * 3.95f ;
        }

		public void CustomDestroy()
		{

            SpriteManager.RemoveSprite(Cursor);
            SpriteManager.RemoveSprite(MenuItems);
            SpriteManager.RemoveSprite(MainImageBackground);
            
		}

        private static void CustomLoadStaticContent(string contentManagerName)
        {

        }

	}
}
